package com.ehsunbehravesh.mypasswords;

public class PasswordAnalyser {
  private int points;
  private String password;

  public PasswordAnalyser(String password) {
    this.password = password;
    this.points = -1;
  }

  public int getPoints() {
    if (points == -1)
      calculatePoints();
    return points;
  }

  private void calculatePoints() {
    int pointsOfLength = 0;
    int pointsOfDifferentChars = 0;

    if (password.length() > 4)
      pointsOfLength = 1;
    if (password.length() > 6)
      pointsOfLength = 2;
    if (password.length() > 8)
      pointsOfLength = 4;
    if (password.length() > 10)
      pointsOfLength = 5;

    int differentChars = calcDifferentChars(password);
    if (differentChars > 1)
      pointsOfDifferentChars = 1;
    if (differentChars > 2)
      pointsOfDifferentChars = 4;
    if (differentChars > 3)
      pointsOfDifferentChars = 6;

    points = pointsOfLength + pointsOfDifferentChars;
  }

  private int calcDifferentChars(String password) {
    String alphabets = PasswordGenerator.ALPHABETS.substring(0, PasswordGenerator.ALPHABETS.length() / 2);
    String capitalAlphabets = PasswordGenerator.ALPHABETS.substring(PasswordGenerator.ALPHABETS.length() / 2);

    int hasAlphabets = anyExists(password, alphabets);
    int hasCapitalAlphabets = anyExists(password, capitalAlphabets);
    int hasNumbers = anyExists(password, PasswordGenerator.NUMBERS);
    int hasSymbols = anyExists(password, PasswordGenerator.SYMBOLS);

    return hasAlphabets + hasCapitalAlphabets + hasNumbers + hasSymbols;
  }

  private int anyExists(String text, String chars) {
    for (int i = 0; i < text.length(); i++) {
      if (chars.indexOf(text.charAt(i)) >= 0)
        return 1;
    }
    return 0;
  }
}
